import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      component: () => import('@/views/Layout.vue'),
      children: [
        {
          path: '',
          name: 'Home',
          component: () => import('@/views/Home.vue'),
        },
        {
          path: 'upload',
          name: 'Upload',
          component: () => import('@/views/Upload.vue'),
        },
        {
          path: 'ai',
          name: 'AI',
          component: () => import('@/views/PictureAI.vue'),
        },
        {
          path: '/profile',
          name: 'Profile',
          component: () => import('@/views/Profile.vue'),
        },
        {
          path: 'picture/:id',
          name: 'Picture',
          component: () => import('@/views/PictureDetail.vue'),
        },
        {
          path: 'space',
          name: 'Space',
          component: () => import('@/views/Space.vue'),
        },
        {
          path: 'space_user',
          name: 'SpaceUser',
          component: () => import('@/views/SpaceUser.vue'),
        },
        {
          path: '/space-analysis',
          name: 'SpaceAnalysis',
          component: () => import('@/views/spaceDetail/SpaceAnalysis.vue'),
        },
        {
          path: 'space_detail',
          name: 'SpaceDetail',
          meta: {
            requiresAuth: true,
          },
          redirect: { name: 'PrivateSpaceDetail' },
          component: () => import('@/views/spaceDetail/SpaceDetail.vue'),
          children: [
            {
              path: 'private_space_detail',
              name: 'PrivateSpaceDetail',
              component: () => import('@/views/spaceDetail/PrivateSpace.vue'),
            },
            {
              path: 'team_space_detail',
              name: 'TeanSpaceDetail',
              component: () => import('@/views/spaceDetail/TeamSpace.vue'),
            },
          ],
        },
        {
          path: 'picture_search',
          name: 'PictureSearch',
          component: () => import('@/views/PictureSearch.vue'),
        },
        {
          path: 'picture_editor',
          name: 'PictureEditor',
          component: () => import('@/views/ImageEditors.vue'),
        },
      ]
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import('@/views/Login.vue'),
    },
    {
      path: '/admin',
      component: () => import('@/views/admin/Admin.vue'),
      redirect: { name: 'AdminUser' },
      meta: {
        requireAuth: true
      },
      children: [
        {
          path: 'users',
          name: 'AdminUser',
          component: () => import('@/views/admin/UserManagement.vue'),
          meta: {
            requireAuth: true
          },
        },
        {
          path: 'pictures',
          name: 'AdminPicture',
          component: () => import('@/views/admin/PictureManagement.vue'),
          meta: {
            requireAuth: true
          },
        },
        {
          path: 'categories',
          name: 'AdminCategory',
          component: () => import('@/views/admin/ClassifyManagement.vue'),
          meta: {
            requiresAuth: true,
          },
        },
        {
          path: 'spaces',
          name: 'AdminSpace',
          component: () => import('@/views/admin/SpaceManagement.vue'),
          meta: {
            requiresAuth: true,
          },
        },
        // 空间分析
        {
          path: 'space_analysis',
          name: 'AdminSpaceAnalysis',
          component: () => import('@/views/admin/SpaceAnalyze.vue'),
          meta: {
            requiresAuth: true,
          },
        }
      ]
    }
  ],
})

router.beforeEach((to, from, next) => {
  const userInfo = localStorage.getItem('userInfo')
  if (to.meta.requiresAuth && !userInfo) {
    next({
      path: '/login',
    })
  } else {
    next()
  }
})

export default router
